{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6f9c2a29-b696-4f67-b635-0b12db0fcca7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个数:  2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组: [1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n"
     ]
    }
   ],
   "source": [
    "def insert_sorted(arr, num):\n",
    "    # 创建一个新的数组来存储结果\n",
    "    new_arr = []\n",
    "    inserted = False\n",
    "    \n",
    "    for item in arr:\n",
    "        if not inserted:\n",
    "            if item >= num:\n",
    "                # 在这里插入num\n",
    "                new_arr.append(num)\n",
    "                inserted = True\n",
    "            new_arr.append(item)\n",
    "        else:\n",
    "            # 插入之后，只需继续添加剩余的元素\n",
    "            new_arr.append(item)\n",
    "    \n",
    "    # 如果num比数组中的所有元素都大，它还没有被插入\n",
    "    if not inserted:\n",
    "        new_arr.append(num)\n",
    "    \n",
    "    return new_arr\n",
    "\n",
    "# 示例已排序数组\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "\n",
    "# 从键盘读取输入\n",
    "num = int(input(\"请输入一个数: \"))\n",
    "\n",
    "# 插入并打印结果\n",
    "result_array = insert_sorted(sorted_array, num)\n",
    "print(\"插入后的数组:\", result_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d28765b1-bd81-40d9-bb44-485b8034013c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
